<!--心率呼吸图-->
<template>
  <div id="heart-beat-chart" class="chart">
    <div class="title-div">
      <span class="title">心率呼吸图</span>
      <span class="unit">次/非分钟</span>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {nextTick, onMounted, watch} from 'vue';
import * as echarts from 'echarts';

type EChartsOption = echarts.EChartsOption;

const props = defineProps({
  chartData: {
    historyTime: [] as any,
    heartbeat: [] as any,
    breathing: [] as any,
    temperature: [] as any,
    bloodOxygen: [] as any,
  },
});

onMounted(() => {
  nextTick(() => {
    init();
  })
});

watch(
  () => props.chartData.historyTime,
  (v) => {
    init();
  },
  {deep: true},
);
/**
 * 初始化函数
 * */
const init = async () => {
  let chartDom = document.getElementById('heart-beat-chart');
  const {historyTime, heartbeat, breathing, temperature, bloodOxygen} = props.chartData
  console.log('props.chartData', {historyTime, heartbeat, breathing, bloodOxygen})
  let myChart = echarts.init(chartDom);
  let option: EChartsOption;
  option = {
    legend: {
      data: ['心率', '呼吸', '体温', '血氧'],
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: historyTime
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '心率',
        data: heartbeat,
        type: 'line',
        smooth: true
      },
      {
        name: '呼吸',
        data: breathing,
        type: 'line',
        smooth: true
      },
      {
        name: '体温',
        data: temperature,
        type: 'line',
        smooth: true
      },
      {
        name: '血氧',
        data: bloodOxygen,
        type: 'line',
        smooth: true
      }
    ]
  };
  option && myChart.setOption(option);
};
</script>

<style lang="less" scoped>
.chart {
  position: relative;
  border: 1px solid rgba(198, 202, 209, 0.5);
  border-radius: 8px;
  padding: 40px 20px;
  height: 420px;

  .title-div {
    position: absolute;
    top: 10px;
    left: 20px;
    display: flex;
    flex-direction: column;

    .title {
      font-weight: bold;
      color: rgba(0, 0, 0, 0.6);
      margin-bottom: 10px;
    }

    .unit {
      color: #8b96a6;
      font-size: 12px;
    }
  }
}
</style>
